package ctm.javacode;

import java.io.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.lang.*;
import java.net.URLEncoder;

import com.mysql.jdbc.StringUtils;

@WebServlet("/suggestSearch.do")
public class suggestSearch extends HttpServlet {

	/****** 資料庫變數 ******/
	String sqlQuery = "";
	ResultSet rs;
	dbBean db = new dbBean();
	int displaycount = 10;  //設置每頁顯示10筆
	ArrayList<String> save ; 
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		/****** 設定 ******/
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();
		PrintWriter out = response.getWriter();// Write the response back to the browser
		
		String type = request.getParameter("type");
		String Id = request.getParameter("id"); //身分證字號字號
		int count = Integer.parseInt(request.getParameter("count")); //目前頁面顯示位置
		save = new ArrayList<String>();
		
		ArrayList<String> time_list = new ArrayList<String>(); //填寫時間列表
		time_list = getdatabase(Id);
		int displaynum = time_list.size(); //最終每頁顯示數量

		switch (type) {
		case "suggestsearch":
			String data = "";
			if (time_list.size() > displaycount){
				out.print("nextpage:inline|"); //顯示nextpage
				out.print(displaycount+"|");
			}
			
			if ((count+displaycount) < displaynum)
				displaynum = count + displaycount;
			
			data = "<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">刪除資料</td>" 
				+ "<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td></tr>";
			for (int i=0 ; i<displaynum ; i++){
				data = data + "<tr><td align="+"\"center\"><input type=\"checkbox\" value=\"" + Id + "," + time_list.get(i) + "\" class=\"checkbox\" /></td>"
						+ "<td align="+"\"center\">" 
						+ "<a href="+"\"suggestHistory.jsp?time="+time_list.get(i)
						+"&name="+Id+"\" target=\"_blank\">";
				if (save.get(i).equals("F")) {
					data = data + time_list.get(i)+ "(暫)</a></td></tr>";
				}
				else {
					data = data + time_list.get(i)+ "</a></td></tr>";
				}
			}
			System.out.println(data);
			out.print(data);
			
			break;
			
		case "nextpage":
			if ((count+displaycount) >= time_list.size()){
				displaynum = time_list.size();
				out.print("nextpage:none|"); //不顯示nextpage
			}
			else{
				displaynum = count + displaycount;
				out.print("nextpage:inline|"); //顯示nextpage
			}
			out.print("prevouspage:inline|"); //顯示prevouspage
			out.print(displaynum+"|");

			data = "<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">刪除資料</td>" 
					+ "<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td></tr>";
			for (int i=count ; i<displaynum ; i++){
				data = data + "<tr><td align="+"\"center\"><input type=\"checkbox\" value=\"" + Id + "," + time_list.get(i) + "\" class=\"checkbox\" /></td>"
						+ "<td align="+"\"center\""+">" 
						+ "<a href="+"\"suggestHistory.jsp?time="+time_list.get(i)
						+"&name="+Id+"\" target=\"_blank\">";
				if (save.get(i).equals("F")) {
					data = data + time_list.get(i)+ "(暫)</a></td></tr>";
				}
				else {
					data = data + time_list.get(i)+ "</a></td></tr>";
				}
			}
			System.out.println(data);
			out.print(data);
			
			break;
			
		case "prevouspage":
			if (count == time_list.size()){
				if ((count % displaycount) == 0)
					displaynum = count - displaycount;
				else
					displaynum = count - (count % displaycount);
			}else{
				displaynum = count - displaycount;
			}
			
			
			out.print("nextpage:inline|"); //顯示nextpage
			if ((displaynum-displaycount) <= 0){
				out.print("prevouspage:none|"); //不顯示prevouspage
			}else{
				out.print("prevouspage:inline|"); //顯示prevouspage
			}		
			out.print(displaynum+"|");			

			data = "<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">刪除資料</td>" 
					+ "<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td></tr>";
			for (int i=displaynum-displaycount ; i<displaynum ; i++){
				data = data + "<tr><td align="+"\"center\"><input type=\"checkbox\" value=\"" + Id + "," + time_list.get(i) + "\" class=\"checkbox\" /></td>"
						+ "<td align="+"\"center\""+">" 
						+ "<a href="+"\"suggestHistory.jsp?time="+time_list.get(i)
						+"&name="+Id+"\" target=\"_blank\">";
				if (save.get(i).equals("F")) {
					data = data + time_list.get(i)+ "(暫)</a></td></tr>";
				}
				else {
					data = data + time_list.get(i)+ "</a></td></tr>";
				}
			}
			System.out.println(data);
			out.print(data);
			
			break;
			
		//delet data
		case "deldata":
			String datalist = request.getParameter("datalist");
			String str_deldata = "";
			String[] data_array;
			data_array = datalist.split(";");
			db.Connected();			
			try {
				System.out.println(data_array);
				for (int i = 0; i < data_array.length; i++) {
					String[] str = data_array[i].split(",");	
					str_deldata = "DELETE FROM `history` WHERE id='" + str[0].trim() + "' && time='" + str[1].trim() + "'";					
					db.ExcuteUpdate(str_deldata);
				}
				db.CloseConn();
				out.print("succ");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				out.print("error");
			}
			break;
		}
		
		out.close();
	}

	private ArrayList<String> getdatabase(String Id){
		ArrayList<String> time_list = new ArrayList<String>(); //填寫時間列表
		sqlQuery = "SELECT * FROM `history` where id='" + Id + "' order by num desc";
		try {
			rs = db.ExcuteQuery(sqlQuery);
			while (rs.next()){
				time_list.add(rs.getString("time"));
				save.add(rs.getString("save"));
			}			
			db.CloseConn();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return time_list; //temp : [ ArrayList [time_list] , ArrayList [category_list] ]
	}
	
}
